<?php
General::getTpl()->assign('extraTitle',"Smilies");

if(!Admin::checkPermission('canManageSmilies')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	require_once './kernel/smilies.php';
	
	
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		// New BBCode?
		require_once 'kernel/form.php';
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Eingabe";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[1]['fieldName'] = "Smilies";
		$formField[1]['fieldType'] = Form::$TYPE_FILE;
		$formField[1]['fieldDefaultValue'] = "";
		$formField[1]['fieldCheck'] = true;
		$formField[1]['fieldCheckExtra'] = "both";
		$formField[1]['fieldFileMaxSize'] = 300;
		$formField[1]['fieldFileMimeTypes'] = "gif,jpeg,jpg,png";
		
		$formField[2]['fieldName'] = "Smilie anzeigen?";
		$formField[2]['fieldDescription'] = "Soll der Smilie in der &Uuml;bersicht angezeigt werden, oder nur im Popup?";
		$formField[2]['formName'] = "smilieDisplay";
		$formField[2]['fieldType'] = Form::$TYPE_SELECT;
		$formField[2]['fieldDefaultValue'] = "";
		$formField[2]['fieldCheck'] = false;
		$formField[2]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[2]['fieldSelectMultiple'] = false;
		
		$form = new Form();
		$form->initByData("Smilie hochladen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Upload Smilie
			move_uploaded_file($_FILES['Smilies']['tmp_name'],"./smilies/".$_FILES['Smilies']['name']);
			$smilieDisplay = ($_POST['smilieDisplay'][0] == "Ja") ? 0:1;
			Smilies::getInstance()->addSmilie($_POST['Eingabe'],$smilieDisplay,$_FILES['Smilies']['name']);
			
			General::ouputMessage('Erfolgreich','Der Smilie wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}	
	
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		// Get Smilie-Data
		$data = Smilies::getInstance()->getSmilie($_GET['id']);
		
		// New BBCode?
		require_once 'kernel/form.php';
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Eingabe";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = $data['smilieInput'];
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[1]['fieldName'] = "Smilies";
		$formField[1]['fieldDescription'] = "Wenn ein Bild ausgew&auml;hlt wird, wird das alte ersetzt";
		$formField[1]['fieldType'] = Form::$TYPE_FILE;
		$formField[1]['fieldDefaultValue'] = "";
		$formField[1]['fieldCheck'] = true;
		$formField[1]['fieldCheckExtra'] = "both";
		$formField[1]['fieldFileMaxSize'] = 300;
		$formField[1]['fieldFileMimeTypes'] = "gif,jpeg,jpg,png";
		
		$formField[2]['fieldName'] = "Smilie anzeigen?";
		$formField[2]['fieldDescription'] = "Soll der Smilie in der &Uuml;bersicht angezeigt werden, oder nur im Popup?";
		$formField[2]['formName'] = "smilieDisplay";
		$formField[2]['fieldType'] = Form::$TYPE_SELECT;
		$formField[2]['fieldDefaultValue'] = ($data['smilieDisplay'] == 0) ? 'Ja':'Nein';
		$formField[2]['fieldCheck'] = false;
		$formField[2]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[2]['fieldSelectMultiple'] = false;
		
		$form = new Form();
		$form->initByData("Smilie bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Upload Smilie
			$smilieFile = $data['smilieFile'];
			if($_FILES['Smilies']['name'] != "") {
				// Delete Old Image
				if(file_exists("./smilies/".$smilieFile)) {
					unlink("./smilies/".$smilieFile);
				}
				
				move_uploaded_file($_FILES['Smilies']['tmp_name'],"./smilies/".$_FILES['Smilies']['name']);
				$smilieFile = $_FILES['Smilies']['name'];
			}
			//move_uploaded_file($_FILES['Smilies']['tmp_name'],"./smilies/".$_FILES['Smilies']['name']);
			$smilieDisplay = ($_POST['smilieDisplay'][0] == "Ja") ? 0:1;
			Smilies::getInstance()->editSmilie($_GET['id'],$_POST['Eingabe'],$smilieDisplay,$smilieFile);
			
			General::ouputMessage('Erfolgreich','Der Smilie wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?<br />","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Smilies::deleteSmilie($_GET['id']);
				General::ouputMessage('Erfolgreich','Der Smilie wurde erfolgreich gel&ouml;scht.','form');
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	General::getTpl()->assign('smilies',
		Smilies::getInstance()->getSmilies());
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/smilies.html'));
}
?>